<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Application_Model_Abstract
 *
 * @author Eugene
 */
abstract class Application_Model_Abstract
{

	/**
	 *
	 * @param array $options
	 * @return void
	 */
	public function __construct(array $options = null)
	{
		if ($options) {
			$this->setOptions($options);
		}
	}

	/**
	 *
	 * @param string $name
	 * @param mixed $value
	 * @return void
	 */
	public function __set($name, $value)
	{
		$method = 'set'.$name;

		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid property');
		}

		$this->$method($value);
	}

	/**
	 *
	 * @param string $name
	 * @return mixed
	 */
	public function __get($name)
	{
		$method = 'get'.$name;

		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid property');
		}

		return $this->$method();
	}

	/**
	 *
	 * @param array $options
	 * @return Core_Model_Abstract
	 */
	public function setOptions(array $options)
	{
		$methods = get_class_methods($this);

		foreach ($options as $key => $value) {
			$method = 'set'.ucfirst($key);

			if (in_array($method, $methods)) {
				$this->$method($value);
			}
		}

		return $this;
	}

}